// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { UINames } from "../../Enums/UIEnum";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import PlatformManager from "../../Platforms/PlatformManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RechargeItem extends cc.Component {


    async buyClick(e: cc.Event, num: string) {
        var age = SaveManager.single.age;
        if (age >= 18) {
            var usedata = SaveManager.single.userdata;
            if (num == "128" || num == "328" || num == "648") {
                usedata.diamond += Number(num) * 100 * 2;
                UIManager.single.showTips("钻石+" + Number(num) * 100 * 2);
            } else {
                usedata.diamond += Number(num) * 100;
                UIManager.single.showTips("钻石+" + Number(num) * 100);
            }
            SaveManager.single.saveData();
            GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
        } else {
            if (age <= 8) {
                UIManager.single.showUI(UINames.RechargeLimitTipsUI);
            } else {
                var accountName = cc.sys.localStorage.getItem("accountName");
                let result = await PlatformManager.single.basePlatform.sdk.payAmount(accountName, Number(num));
                if (result.code == 500) {
                    UIManager.single.showUI(UINames.RechargeLimitTipsUI);
                    return;
                }
                if (age < 16) {
                    if (Number(num) >= 50 || result.data.payAmount.toMonthAmount >= 200) {
                        UIManager.single.showUI(UINames.RechargeLimitTipsUI);
                        return;
                    }
                } else {
                    if (Number(num) >= 100 || result.data.payAmount.toMonthAmount >= 400) {
                        UIManager.single.showUI(UINames.RechargeLimitTipsUI);
                        return;
                    }
                }

                var usedata = SaveManager.single.userdata;
                if (num == "128" || num == "328" || num == "648") {
                    usedata.diamond += Number(num) * 100 * 2;
                    UIManager.single.showTips("钻石+" + Number(num) * 100 * 2);
                } else {
                    usedata.diamond += Number(num) * 100;
                    UIManager.single.showTips("钻石+" + Number(num) * 100);
                }
                SaveManager.single.saveData();
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
            }


        }
    }
}
